/*
 * highScore.cpp
 *
 *  Created on: Apr 25, 2010
 *      Author: chincmex
 */

#include "libraries.h"

using namespace std;

highScore::highScore()
{
	readFile();
}

highScore::~highScore()
{
	delete [] scoreArray;
}

void highScore::printScreen(SDL_Surface*& scrn)
{
	for(int i = 0; i < SLOTS; i++)
	{
		scoreTable[i].draw(scrn);
		nameTable[i].draw(scrn);
	}
}
/*
void highScore::getGamertag(Timer &pause)
{
	outData.close();
	outData.clear();
	outData.open("newPlayer.txt", ios::out);

	if(pause.get_ticks() > 300)
	{
		pause.start();
	}
}*/


//DO NOT PUT IN A LOOP
void highScore::updateScore(int inScore, string newName = "FAIL")
{
	if(inScore >= 999999)
	{
		inScore = 999999;
	}

	//Apparently there's no itoa in GCC in linux =(
	ssTemp.flush();
	ssTemp.clear();
	ssTemp << inScore;
	ssTemp >> newScore;
	ssTemp.flush();
	ssTemp.clear();


	cout << newScore << endl;

	if(newName.empty())
	{
		newName = "FAIL";
	}
	if(newName.size() > 4)
	{
		newName = "FAIL";
	}

	pair<string, string> temp = make_pair(newScore,newName);

	for(int i = 0; i < SLOTS; i++)
	{
		if(atoi(scoreArray[i].first.c_str()) < atoi(temp.first.c_str()))
		{
			for(int j = (SLOTS - 1); j > i; j--)
			{
				scoreArray[j] = scoreArray[j-1];
			}

			sScore.flush();
			sScore.clear();
			sScore << setw(6) << setfill('0');
			sScore << temp.first;
			sScore >> temp.first;
			sScore.flush();
			sScore.clear();

			sName.flush();
			sName.clear();
			sName << setw(4) << setfill('-');
			sName << temp.second;
			sName >> temp.second;
			sName.flush();
			sName.clear();

			scoreArray[i] = temp;


			i = SLOTS;
		}
	}

	updateFile();
}


void highScore::readFile()
{
	try
	{
		inData.close();
		inData.clear();
		inData.open("tScr.txt", ios::in);
		if(!inData)
		{
			throw "MISSING OR CORRUPTED FILE";
		}

		inData >> temp;

		if(temp != "centipedeDaemonia")
		{
			throw "MISSING OR CORRUPTED FILE";
		}

		scoreTable.erase();
		nameTable.erase();

		scoreArray = new pair<string, string>[SLOTS];

		for(int i = 0; i < SLOTS; i++)
		{
			inData >> scoreArray[i].second >> scoreArray[i].first;

			temp = scoreArray[i].second;
			scoreTable.pushBack(TextElement(temp, X_POS_NAME, (Y_POS + (i * 57)), 255, 255, 255, 30, "center", "freesansbold.tff"));

			temp = scoreArray[i].first;
			nameTable.pushBack(TextElement(temp, X_POS_SCOR, (Y_POS + (i * 57)), 255, 255, 255, 30, "center", "freesansbold.tff"));
		}

	}
	catch(const char *s)
	{
		makeFile();
	}
}

void highScore::makeFile()
{
	scoreArray = new pair<string, string>[SLOTS];

	for(int i = 0; i < SLOTS; i++)
	{
		scoreArray[i].first  = "000000";
		scoreArray[i].second = "----";
	}

	updateFile();
}

void highScore::updateFile()
{

	outData.close();
	outData.clear();
	outData.open("tScr.txt", ios::out);

	outData << "centipedeDaemonia" << endl;

	for(int i = 0; i < SLOTS; i++)
	{
		outData << scoreArray[i].second << endl;
		outData << scoreArray[i].first << endl;
	}
	scoreTable.erase();
	nameTable.erase();
	for(int i = 0; i < SLOTS; i++)
	{
		temp = scoreArray[i].second;
		scoreTable.pushBack(TextElement(temp, X_POS_NAME, (Y_POS + (i * 57)), 255, 255, 255, 30, "center", "freesansbold.tff"));

		temp = scoreArray[i].first;
		nameTable.pushBack(TextElement(temp, X_POS_SCOR, (Y_POS + (i * 57)), 255, 255, 255, 30, "center", "freesansbold.tff"));
	}

	outData.close();
}
